package com.ksd.modules.special.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.modules.special.entity.AdminSpecialCode;
import com.ksd.modules.special.mapper.AdminSpecialCodeMapper;
import com.ksd.modules.special.po.AdminSpecialCodeSavePO;
import com.ksd.modules.special.po.AdminSpecialCodeSearchPO;
import com.ksd.modules.special.service.IAdminSpecialCodeService;
import com.ksd.modules.special.vo.AdminSpecialCodeVo;
import com.ksd.utils.snow.SnowflakeIdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AdminSpecialCodeServiceImpl extends BaseServiceImpl<AdminSpecialCodeMapper, AdminSpecialCode> implements IAdminSpecialCodeService {
    /**
     * 查询列表
     * @param adminSpecialCodeSearchPO
     * @return
     */
    public Page<AdminSpecialCodeVo> findAdminSpecialCodes(AdminSpecialCodeSearchPO adminSpecialCodeSearchPO){
        Page<AdminSpecialCode> page = new Page<>(adminSpecialCodeSearchPO.getPageNo(),adminSpecialCodeSearchPO.getPageSize());
        LambdaQueryWrapper<AdminSpecialCode> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminSpecialCode::getSystemId,SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(Objects.nonNull(adminSpecialCodeSearchPO.getOpid()),AdminSpecialCode::getOpid,adminSpecialCodeSearchPO.getOpid());
        lambdaQueryWrapper.eq(Objects.nonNull(adminSpecialCodeSearchPO.getMark()),AdminSpecialCode::getMark,adminSpecialCodeSearchPO.getMark());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminSpecialCodeSearchPO.getCode()), AdminSpecialCode::getCode,adminSpecialCodeSearchPO.getCode());
        lambdaQueryWrapper.orderByDesc(AdminSpecialCode::getCreateTime);
        Page<AdminSpecialCode> adminSpecialPage = this.page(page, lambdaQueryWrapper);
        Page<AdminSpecialCodeVo> adminSpecialVoPage = tranferPageBo(adminSpecialPage, AdminSpecialCodeVo.class);
        return adminSpecialVoPage;
    }



    /**
     * 创建兑换码
     * @param adminSpecialCodeSavePO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean createBatchCode(AdminSpecialCodeSavePO adminSpecialCodeSavePO){
        AdminSpecialCode adminUserWalletCode= tranferBo(adminSpecialCodeSavePO, AdminSpecialCode.class);
        adminUserWalletCode.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminUserWalletCode.setSystemName(SystemIdThrealLocal.get().getSystemName());
        adminUserWalletCode.setCode(new SnowflakeIdWorker(9,8).nextId()+"");
        adminUserWalletCode.setMark(0);
        this.saveOrUpdate(adminUserWalletCode);
        return true;
    }


    /**
     * 单个物理删除
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean removeAdminSpecialCodeById(Long id){
        boolean b = this.removeById(id);
        return b;
    }


    /**
     * 批量物理删除
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchRemovesAdminSpecialCode(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        return b;
    }


    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean updateStatus(Long id,String column,Integer value){
        boolean b = super.updateStatus(id,column,value);
        return b;
    }

    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    public boolean updateStatusStr(Long id,String column,String value){
        boolean b = super.updateStatusStr(id,column,value);
        return b;
    }

}
